﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace $rootnamespace$.Areas.Wikimentation.Models
{
    public class WikiPage
    {

        string _path;
        string _pathWithFile;

        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime? LastChange { get; set; }
        public Guid Id { get; set; }

        public WikiPage(string path)
        {
            _path = path;
            _pathWithFile = Path.Combine(_path, "maincontent.xml");
        }

        public WikiPage()
        {
            _path = "";
            _pathWithFile = Path.Combine(_path, "maincontent.xml");
        }
        /// <summary>
        /// Loads all Wikis
        /// </summary>
        /// <returns></returns>
        public List<WikiPage> GetWikiPages()
        {
            var pages = new List<WikiPage>();

            if (System.IO.File.Exists(_pathWithFile))
            {
                pages = SimpleXmlFileHelper<List<WikiPage>>.Load(_pathWithFile);
            }
            else
            {
                var initpages = new List<WikiPage>{
                    new WikiPage{ Title="Testheader 1", Content="here is the docu-Content", LastChange=DateTime.Now, Id=Guid.NewGuid()},
                    new WikiPage{ Title="Testheader 2", Content="Lorem ipsum", LastChange=DateTime.Now,Id=Guid.NewGuid()}
                };

                SaveWikiPages(initpages);
                pages = initpages;
            }

            return pages;
        }
        public WikiPage GetWikiPage(Guid guid)
        {
            var page = GetWikiPages().Where(x => x.Id == guid);
            return page.FirstOrDefault();
        }
        public IEnumerable<WikiPage> UpdateWikiPage(WikiPage wikiPage)
        {
            var allPages = GetWikiPages();
            var pagesToUpdate = allPages.Where(x => x.Id == wikiPage.Id);
            if (pagesToUpdate.Any())
            {
                pagesToUpdate.First().Content = wikiPage.Content;
                pagesToUpdate.First().Title = wikiPage.Title;
            }
            else
            {
                allPages.ToList().Add(new WikiPage { Content = wikiPage.Content, Title = wikiPage.Title, LastChange = DateTime.Now, Id = Guid.NewGuid() });
            }
            SaveWikiPages(allPages);

            return allPages;

        }

        public WikiPage CreateWikiPage()
        {
            var allPages = GetWikiPages();
            var newPage = new WikiPage { Title = "new Page", Content = "Content, *Bold Content*", LastChange = DateTime.Now, Id = Guid.NewGuid() };
            allPages.Add(newPage);

            SaveWikiPages(allPages);

            return newPage;
        }



        public void SaveWikiPages(List<WikiPage> pages)
        {
            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }
            SimpleXmlFileHelper<List<WikiPage>>.Save(pages.ToList(), _pathWithFile);
        }
    }
}